<template>
  <div class="about">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>组织管理</el-breadcrumb-item>
      <el-breadcrumb-item>部门管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索与添加部门区 -->
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            type="text"
            placeholder="请输入部门名称"
            v-model="info.departmentName"
            clearable
          >
            <el-button
              @click="getUserList"
              slot="append"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="4"
          ><el-button type="primary" @click="addDialogVisible = true"
            >添加部门</el-button
          ></el-col
        >
      </el-row>
      <!-- 部门列表区 -->
      <el-table border stripe :data="userList">
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column label="部门名称" prop="departmentName">
        </el-table-column>
        <el-table-column label="联系电话" prop="contactPhone">
        </el-table-column>
        <el-table-column label="邮箱" prop="email"> </el-table-column>
        <el-table-column label="备注" prop="remark"> </el-table-column>
        <el-table-column label="简介" prop="brief"> </el-table-column>
        <el-table-column label="操作" width="145px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="showEdit(scope.row.id)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="removeId(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="info.currentPage"
        :page-sizes="[2, 8, 15, 100]"
        :page-size="info.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 添加部门信息 -->
      <el-dialog
        title="添加部门信息"
        :visible.sync="addDialogVisible"
        width="30%"
        @close="addDialogClosed"
      >
        <!-- 内容主题区 -->
        <el-form
          :model="addForm"
          :rules="addFormRules"
          ref="addFormRef"
          label-width="100px"
        >
          <el-form-item label="部门名称" prop="departmentName">
            <!-- <el-select v-model="addForm.department">
              <el-option
                v-for="item in bum"
                :key="item.id"
                :label="item.name"
                :value="item"
              >
              </el-option>
            </el-select> -->
             <el-input v-model="addForm.departmentName">
            </el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="addForm.contactPhone"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="addForm.email"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="addForm.remark"></el-input>
          </el-form-item>
          <el-form-item label="简介" prop="brief">
            <el-input v-model="addForm.brief"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addBum">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 编辑部门信息 -->
      <el-dialog
        title="修改部门信息"
        :visible.sync="editDialogVisible"
        width="30%"
        @close="editDialogClosed"
      >
        <el-form
          :model="editForm"
          :rules="editFormRules"
          ref="editFormRef"
          label-width="90px"
        >
          <el-form-item label="部门名称" prop="departmentName">
            <el-input v-model="editForm.departmentName"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="contactPhone">
            <el-input v-model="editForm.contactPhone"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="editForm.email"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="editForm.remark"></el-input>
          </el-form-item>
          <el-form-item label="简介" prop="brief">
            <el-input v-model="editForm.brief"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editInfo">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    //校验电话号码
    var checkMobile = (rule, value, cb) => {
      //验证手机号码的正则表达式
      const regMobile = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (regMobile.test(value)) {
        return cb();
      }
      cb(new Error("请输入合法的电话号码"));
    };

    //校验邮箱
    var checkEmail = (rule, value, cb) => {
      //验证邮箱的正则表达式
      const regEmail =
        /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
      if (regEmail.test(value)) {
        //合法邮箱
        return cb();
      }
      cb(new Error("请输入合法的邮箱"));
    };
    return {
      //获取部门信息及搜索的请求参数
      info: {
        departmentName: "",
        currentPage: 1,
        size: 4,
      },
      userList: [],
      total: 0,
      // 添加部门对话框隐藏
      addDialogVisible: false,
      //添加部门的表单数据
      addForm: {
        departmentName: "",
        contactPhone: "",
        email: "",
        remark: "",
        brief: "",
      },
      //添加表单的规则验证对象
      addFormRules: {
        departmentName: [
          { required: true, message: "请输入部门名称", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "部门名称为2~20个字符之间",
            trigger: "blur",
          },
        ],
        contactPhone: [
          { required: true, message: "请输入电话号码", trigger: "blur" },
          {
            validator: checkMobile,
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: "请输入邮箱名称", trigger: "blur" },
          {
            validator: checkEmail,
            trigger: "blur",
          },
        ],
        remark: [
          { required: true, message: "请输入备注名称", trigger: "blur" },
          {
            min: 1,
            max: 500,
            message: "备注名称为1~500个字符之间",
            trigger: "blur",
          },
        ],
        brief: [
          { required: true, message: "请输入简介名称", trigger: "blur" },
          {
            min: 1,
            max: 500,
            message: "简介名称为1~500个字符之间",
            trigger: "blur",
          },
        ],
      },
      //编辑部门信息对话框隐藏
      editDialogVisible: false,
      //查询到的部门信息对象
      editForm: {},
      //编辑部门信息规则
      editFormRules: {
        departmentName: [
          { required: true, message: "请输入备注名称", trigger: "blur" },
          {
            min: 2,
            max: 500,
            message: "备注名称为2~500个字符之间",
            trigger: "blur",
          },
        ],
        contactPhone: [
          {
            required: true,
            message: "请输入电话号码",
            trigger: "blur",
          },
          {
            validator: checkMobile,
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
          {
            validator: checkEmail,
            trigger: "blur",
          },
        ],

        remark: [
          { required: true, message: "请输入备注名称", trigger: "blur" },
          {
            min: 2,
            max: 500,
            message: "备注名称为2~500个字符之间",
            trigger: "blur",
          },
        ],
        brief: [
          { required: true, message: "请输入备注名称", trigger: "blur" },
          {
            min: 2,
            max: 500,
            message: "备注名称为2~500个字符之间",
            trigger: "blur",
          },
        ],
      },
      bum: [],
    };
  },
  created() {
    this.getUserList();
    this.department();
  },
  methods: {
    //获取部门信息
    getUserList() {
      // this.$http({
      //   method: "post",
      //   url: "organization/selectDepartments",

      //   data:JSON.stringify(this.info) ,
      // }).then((res) => {
      //   if (res.flag !== true) {
      //     return this.$message.error("获取部门信息失败!");
      //   }
      //   this.userList = res.rows;
      //   this.total = res.total;
      //   console.log(res);
      // });
      // const { data: res } = await
      this.$http
        .get("organization/selectDepartments", { params: this.info })
        .then((res) => {
          console.log(res);
          this.userList = res.data.rows;
          this.total = res.data.total;
          if (res.data.flag !== true) {
            return this.$message.error("获取部门信息失败!");
          }
        });
    },
    //监听信息条数
    handleSizeChange(newSize) {
      // console.log(newSize);
      this.info.size = newSize;
      this.getUserList();
    },
    //监听页数
    handleCurrentChange(newPage) {
      // console.log(newPage);
      this.info.currentPage = newPage;
      this.getUserList();
    },
    //监听添加部门表单的关闭事件
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
    },
    //点击按钮添加部门
    addBum() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        //可以发起添加用户的网络请求
        const { data: res } = await this.$http.post(
          "organization/insertDepartment",
          this.addForm
        );
        if (res.flag !== true) {
          return this.$message.error("添加部门信息失败!");
        }
        this.$message.success("添加部门信息成功!");
        //隐藏添加部门信息的对话框
        this.addDialogVisible = false;
        //重新获取部门信息列表数据
        this.getUserList();
      });
    },

    //点击按钮编辑部门信息
    async showEdit(id) {
      // console.log(id);
      const { data: res } = await this.$http.get(
        "organization/selectDepartmentById",
        { params: { id } }
      );
      if (res.flag !== true) {
        return this.$message.error("获取部门信息失败!");
      }
      // this.$message.success("获取信息成功!");

      this.editForm = res.data;
      this.editDialogVisible = true;
    },
    // 监听修改部门对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    //修改部门信息并提交
    editInfo() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        //发起修改部门信息请求
        const { data: res } = await this.$http.post(
          "organization/updateDepartment",
          {
            id: this.editForm.id,
            departmentName: this.editForm.departmentName,
            contactPhone: this.editForm.contactPhone,
            email: this.editForm.email,
            remark: this.editForm.remark,
            brief: this.editForm.brief,
          }
        );
        if (res.flag !== true) {
          return this.$message.error("修改部门信息失败");
        }
        //关闭对话框
        this.editDialogVisible = false;
        //提示修改信息成功
        this.$message.success("修改部门信息成功");
        //获取新的部门信息
        this.getUserList();
      });
    },
    //点击按钮删除部门信息
    async removeId(id) {
      // console.log(`删除数据的id = ${id}`)

      //弹框询问用户是否删除部门const confirmResult = await
      const confirmResult = await this.$confirm(
        "此操作将永久删除该部门, 是否继续?",
        "删除部门",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      //如果确认删除部门信息，那返回的信息就是字符串confirm
      //如果取消删除部门信息，那返回的信息就是字符串cancel
      // console.log(confirmResult);
      if (confirmResult !== "confirm") {
        return this.$message.info("已经取消删除！");
      }
      const { data: res } = await this.$http.post(
        "organization/deleteDepartment/",
        { id: id }
      );
      console.log(res);

      if (res.flag !== true) {
        return this.$message.error("删除部门信息失败");
      }
      this.$message.success("删除部门信息成功");
      this.getUserList();
    },
    //获取部门信息
    async department() {
      const { data: res } = await this.$http.get(
        "organization/selectDepartmentNames"
      );
      this.bum = res.data;
      console.log(res);
    },
  },
};
</script>

<style scoped></style>
